<template>
  <div class="mb20">
    <div class="pub-table-action-btn" v-if="!disabled">
      <a-button icon="plus" class="btn_right" type="primary" @click="handleAddEditPost('', 1)">新增岗位</a-button>
    </div>
    <a-table
      :loading="loading"
      :pagination="pagination"
      :columns="[
        { title: '序号', width: 60, customRender: (text, record, index) => index + 1 },
        { title: '岗位名称', width: 90, dataIndex: 'name', key: 'name' },
        { title: '岗位编号', dataIndex: 'code', key: 'code' },
        { title: '是否为领导', width: 110, dataIndex: 'isLeader', key: 'isLeader', customRender: val => val == 1 ? '是' : '否' },
        { title: '关联标准岗位', dataIndex: 'basePostName', key: 'basePostName' },
        { title: '岗位标签', dataIndex: 'postLabelName', key: 'postLabelName' },
        { title: '岗位人员', dataIndex: 'employeeNames', key: 'employeeNames' },
        { title: '状态', width: 80, dataIndex: 'enable', key: 'enable', customRender: val => val == 1 ? '可用' : '不可用' },
        ...(!disabled ? [{ title: '操作', width: 80, scopedSlots: { customRender: 'operation' } }] : [])
      ]"
      :data-source="tableData"
      row-key="id"
    >
      <template slot="operation" slot-scope="text, record">
        <div class="pub-table-list-action">
          <span @click="() => handleAddEditPost(record, 2)">编辑</span>
          <!-- <span @click="() => handleDeleteWarning(record)">删除</span> -->
        </div>
      </template>
    </a-table>
    <addEditPost :visible="visible" :postIndex="postIndex" :postInfo="postInfo" @cancel="handleCancel" @submit="handleTable"></addEditPost>
  </div>
</template>

<script>
import { getPostManageList4, deletePostManageListNew } from '@/api/system/system'
import addEditPost from './addEditPost'

export default {
  name: 'PostManage',
  components: {
    addEditPost,
  },
  props: {
    orgId: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      postIndex: 1,
      postInfo: {},
      chooseData: 0,
      tableData: [],
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      selectOptions: {}, // 选中节点对象

      pagination: {
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        showSizeChanger: true,
        showQuickJumper: true,
        pageSizeOptions: ['5', '10', '15', '20'],
        showTotal: (total) => `共 ${total} 条`, // 显示总数
        onShowSizeChange: (current, pageSize) => {
          this.pagination.defaultCurrent = 1
          this.pagination.defaultPageSize = pageSize
          this.handleTable() //显示列表的接口名称
        },
        // 改变每页数量时更新显示
        onChange: (current, size) => {
          this.pagination.defaultCurrent = current
          this.pagination.defaultPageSize = size
          this.handleTable()
        }
      },
    }
  },
  created() {
    this.handleTable()
  },
  methods: {
    /**
     *@desc  table表格列表
     *@param {}
     */
    handleTable() {
      // if (!this.orgId) return
      let _this = this
      let params = {
        resultWithEmployee: true, // 为true时返回岗位人员
        pageNumber: _this.pagination.defaultCurrent,
        pageSize: _this.pagination.defaultPageSize,
        orgId: _this.orgId
      }
      this.loading = true
      getPostManageList4(params).then((res) => {
        this.loading = false
        if (res.status === 200) {
          _this.tableData = res.data
        }
      })
    },
    /**
     *@desc  table表格搜索 暂停查询
     *@param {}
     */
    /**
     *@desc  table新增编辑
     *@param {}
     */
    handleAddEditPost(record, index) {
      this.postIndex = index
      this.postInfo = record == '' ? { orgId: this.orgId } : record
      this.visible = true
    },
    /**
     *@desc  table表格删除
     *@param {}
     */
    handleDeleteWarning(record) {
      let _this = this
      _this.$confirm({
        title: `你确定要删除${record.name}吗？`,
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          _this.handleDelete(record)
        },
        onCancel() {
          // console.log('Cancel');
        }
      })
    },
    handleDelete(record) {
      deletePostManageListNew({ id: record.id }).then((res) => {
        if (res.status === 200) {
          //删除成功
          this.handleTable()
        }else {
          that.$message.warning(res.data.msg)
        }
      })
    },
    handleCancel() {
      this.visible = false;
      this.handleTable()
    }
  }
}
</script>

<style scoped>
</style>
